package com.haidechizi.leetcode;

public class _124_二叉树中的最大路径和 {

    public int maxPathSum(TreeNode root) {
        value(root);
        return max;
    }

    Integer max = Integer.MIN_VALUE;

    public int value(TreeNode treeNode) {
        if (treeNode == null) {
            return 0;
        }
        int lv = Integer.max(value(treeNode.left), 0);
        int rv = Integer.max(value(treeNode.right), 0);

        max = Integer.max(max, treeNode.val + lv + rv);
        return treeNode.val + Integer.max(lv, rv);
    }
}
